package com.wego.controller;

import com.wego.bean.IdName;
import com.wego.bean.TableBean;
import com.wego.common.bean.PageBean;
import com.wego.common.formatter.LocalDateTimeFormatter;
import com.wego.entity.domain.Address;
import com.wego.entity.query.AddressQuery;
import com.wego.entity.vo.AddressVO;
import com.wego.service.AddressService;
import com.wego.service.CountryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;

/**
 * 地址控制器
 *
 * @author Li Chuanwei
 */

@Controller
@RequestMapping("/manager/address")
public class AddressController {

    @Autowired
    private AddressService addressService;

    @Autowired
    private CountryService countryService;

    @GetMapping("/list")
    public @ResponseBody TableBean list(AddressQuery addressQuery) {
        final PageBean<Address> addressPageBean = addressService.selectPage(addressQuery);
        PageBean<AddressVO> pageBean = addressPageBean2AddressVOPageBean(addressPageBean);
        List<AddressVO> addressVOS = pageBean.getResult();
        Long count = pageBean.getTotal();
        TableBean tableBean = new TableBean<AddressVO>();
        tableBean.setCode(0);
        tableBean.setMsg("");
        tableBean.setData(addressVOS);
        tableBean.setCount(count);
        return tableBean;
    }

    private PageBean<AddressVO> addressPageBean2AddressVOPageBean(PageBean<Address> pageBean) {
        PageBean<AddressVO> addressVOPageBean = new PageBean<>();
        addressVOPageBean.setPageNum(pageBean.getPageNum());
        addressVOPageBean.setPageSize(pageBean.getPageSize());
        addressVOPageBean.setPages(pageBean.getPages());
        addressVOPageBean.setTotal(pageBean.getTotal());

        List<AddressVO> list = new ArrayList<>();
        for (Address address : pageBean.getResult()) {
            final AddressVO addressVO = address2AddressVO(address);
            list.add(addressVO);
        }
        addressVOPageBean.setResult(list);

        return addressVOPageBean;
    }

    private AddressVO address2AddressVO(Address address) {
        if (address == null) {
            return null;
        }
        AddressVO addressVO = new AddressVO();
        addressVO.setId(address.getId());
        addressVO.setReceiver(address.getReceiver());

        final List<IdName> list = countryService.selectProvinceCityCountryByCountryId(address.getCountryId());
        AddressVO.list2AddressVO(list, addressVO);

        addressVO.setAddr(address.getAddr());
        addressVO.setPostcode(address.getPostcode());
        addressVO.setPhone(address.getPhone());
        addressVO.setUserId(address.getUserId());
        addressVO.setMoRen(address.getMoRen());
        addressVO.setState(address.getState());
        addressVO.setUpdateTime(LocalDateTimeFormatter.formatLocalDateTime(address.getUpdateTime(),"yyyy-MM-dd HH:mm:ss"));
        return addressVO;
    }

    @GetMapping("/map")
    public String map(String address, Model model){
        model.addAttribute("address",address);
        return "manager/userAddress_Map";
    }

}
